local device,luaj = require("framework.device")
if device.platform == "android" then 
    luaj = require("framework.luaj")
end
local M = {}

function M.tableToStr(_t,indent,tabDescribe)
    indent = indent or 1
    if indent==1 and type(_t)~="table" then
        return (tabDescribe or "").."\n------不是table\n"..tostring(_t)
    end
    local szPrefix = string.rep("    ", indent)
    local szRet = (tabDescribe or "").."\n"..string.rep("    ", indent-1).."{\n"  
    function doT2S(_i, _v) 
        
        szRet = szRet..szPrefix
        if "number" == type(_i) then  
            szRet = szRet .. "[" .. _i .. "] = "  
            if "number" == type(_v) then  
                szRet = szRet .. _v .. ",\n"  
            elseif "boolean" == type(_v) then  
                szRet = szRet .. tostring(_v) .. ",\n"  
            elseif "string" == type(_v) then  
                szRet = szRet .. '"' .. _v .. '"' .. ",\n"  
            elseif "table" == type(_v) then  
                szRet = szRet .. M.tableToStr(_v,indent+1) .. ",\n"  
            else  
                szRet = szRet .. "nil,"  
            end  
        elseif "string" == type(_i) then  
            szRet = szRet ..  _i ..' = '  
            if "number" == type(_v) then  
                szRet = szRet .. _v .. ",\n"  
            elseif "boolean" == type(_v) then  
                szRet = szRet .. tostring(_v) .. ",\n"  
            elseif "string" == type(_v) then  
                szRet = szRet .. '"' .. _v .. '"' .. ",\n"  
            elseif "table" == type(_v) then  
                szRet = szRet .. M.tableToStr(_v,indent+1) .. ",\n"  
            else  
                szRet = szRet .. "nil,"  
            end  
        end  
    end  
    table.foreach(_t, doT2S)  
    szRet = szRet ..string.rep("    ", indent-1).. "}"  
    return szRet
end  

function M.printTab(tab,tabDescribe)
    local str = M.tableToStr(tab,nil,tabDescribe)
    print(str)
end

function M.writeTabToLog(_tab, tabDescribe, logPath, mod)
    M.printTab(_tab,tabDescribe)
    local tabStr = M.tableToStr(tab,nil,tabDescribe)
    if device.platform == "android" then 
        local ok,rootPath = luaj.callStaticMethod("org/cocos2dx/utils/PSNative","getSDcardPath",{},"()Ljava/lang/String;")
        logPath = rootPath.."/"..logPath
        print("logPath",logPath)
    elseif device.platform == "windows" then
        local rootPath = cc.FileUtils:getInstance():getWritablePath()
        logPath = rootPath..logPath
        print("logPath",logPath)
    else
        return
    end
    local mod = mod or 1
    local str = ""
    if mod==1 then
        local oldStr = ""
        local file = io.open(logPath, "r")
        if file then
            oldStr = file:read("*a")
            file:close()
        else
            print("文件打开失败",logPath)
        end
        local temp = "\n"..tabStr
        str = oldStr.."\n\n\n==============================>>>>>>\n\n\n"..temp
    else
        local temp = "\n"..tabStr
        str = temp
    end
    local file = io.open(logPath, "w")
    if file then
        file:write(str)
        file:close()
    else
        print("文件打开失败",logPath)
    end
end

return M